package org.javaymw.springboot.controller;

import org.apache.commons.lang3.RandomStringUtils;
import org.javaymw.springboot.base.BaseController;
import org.javaymw.springboot.event.OnSendEmailEvent;
import org.javaymw.springboot.service.MailService;
import org.javaymw.springboot.service.MemberService;
import org.javaymw.springboot.util.MessageUtils;
import org.javaymw.springboot.vo.MemberVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.thymeleaf.TemplateEngine;
import org.thymeleaf.context.Context;

import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.Map;

/**
 * @Auther: zhaoxinguo
 * @Date: 2018/7/13 10:49
 * @Description: 邮箱验证码
 */
@Controller
@RequestMapping(value = "/email")
public class EmailController extends BaseController {

    @Autowired
    private MailService mailService;

    @Autowired
    private TemplateEngine templateEngine;

    @Autowired
    private MemberService memberService;

    /**
     * 校验邮箱地址是否存在
     * @param email
     * @return
     */
    @RequestMapping(value = "/check", method = RequestMethod.GET)
    @ResponseBody
    public Map<String, Object> check(@RequestParam("email") String email) {
        Map<String, Object> retMap = null;
        try {
            if (memberService.getMemberCountByEmail(email) > 0) {
                retMap = MessageUtils.success();
            } else {
                retMap = MessageUtils.error("邮箱地址不存在，请输入注册时候的邮箱地址");
            }
        } catch (Exception e) {
            retMap = MessageUtils.error();
        }
        return retMap;
    }

    @RequestMapping(value = "/code", method = RequestMethod.GET)
    @ResponseBody
    public Map<String, Object> code(@RequestParam("to") String to, HttpServletRequest request) {
        Map<String, Object> retMap = null;
        try {
            // 解析模板并发送
            Context context = new Context();
            // 生成邮件验证码
            String code = getCode();
            // 把邮件验证码(key)，邮件验证码(value)放到缓存里面
            context.setVariable("code", code);
            String subject = "主题：验证码获取邮件";
            String emailContent = templateEngine.process("front/email/emailTemplate", context);
            MemberVo memberVo = new MemberVo();
            // 提交发送邮件验证码事件
            eventPublisher.publishEvent(new OnSendEmailEvent(memberVo, to, subject, emailContent));
            retMap = MessageUtils.success("验证码已经发送到您的邮箱，请登录邮箱查收");
        } catch (Exception e) {
            retMap = MessageUtils.error();
        }
        return retMap;
    }

    private String getCode() {
        synchronized (this) {
            // 生成4位随机验证码
            String emailCode = RandomStringUtils.random(4,"0123456789");
            // 把邮件验证码放入本地缓存
            cacheManager.getCache("EmailCodeCache").put("emailCode", emailCode);
            return emailCode;
        }
    }
}
